using System.IO;
using BasicScript;
using Game.Core;
using Game.Main.Model;
using Game.Utils;
using UnityEngine;

namespace Game.Manager
{
	public class SaveManager : Singleton<SaveManager>
	{
		private const string GAME_VERSION = "gameVersion";

		private const string MAP_VIEW_LAMP_OPEN = "mapViewLampOpen";

		private const string IS_UNLOCK = "isUnlock";

		private const string PRICE_1 = "price1";

		public string gameVersion
		{
			get
			{
				return GetString("gameVersion");
			}
			set
			{
				SaveString("gameVersion", value);
			}
		}

		public string price1
		{
			get
			{
				return GetString("price1");
			}
			set
			{
				SaveString("price1", value);
			}
		}

		public bool IsMapViewLampOpen
		{
			get
			{
				return GetInt("mapViewLampOpen") != 0;
			}
			set
			{
				SaveInt("mapViewLampOpen", value ? 1 : 0);
			}
		}

		public bool IsUnlock
		{
			get
			{
				return (GetInt("isUnlock") != 0) ? true : false;
			}
			set
			{
				SaveInt("isUnlock", value ? 1 : 0);
			}
		}

		public byte[] LoadMapBasicData()
		{
			return LoadData(ClientConfig.GetPersistentPath(GetBasicDataKey()));
		}

		public void SaveMapBasicData(string data)
		{
			WriteData(ClientConfig.GetPersistentPath(GetBasicDataKey()), Compressor.Compress(data));
		}

		public byte[] LoadMapArchive(int id)
		{
			return LoadData(ClientConfig.GetPersistentPath(GetMapKey(id)));
		}

		public void SaveMapArchive(int id, string data)
		{
			WriteData(ClientConfig.GetPersistentPath(GetMapKey(id)), Compressor.Compress(data));
		}

		public byte[] LoadElevatorArchive()
		{
			return LoadData(ClientConfig.GetPersistentPath(GetElevatorKey()));
		}

		public void SaveElevatorArchive(string data)
		{
			WriteData(ClientConfig.GetPersistentPath(GetElevatorKey()), Compressor.Compress(data));
		}

		public byte[] LoadAmbulanceArchive()
		{
			return LoadData(ClientConfig.GetPersistentPath(GetAmbulanceKey()));
		}

		public void SaveAmbulanceArchive(string data)
		{
			WriteData(ClientConfig.GetPersistentPath(GetAmbulanceKey()), Compressor.Compress(data));
		}

		public byte[] LoadStorageBoxArchive()
		{
			return LoadData(ClientConfig.GetPersistentPath(GetStorageBoxKey()));
		}

		public void SaveStorageBoxArchive(string data)
		{
			WriteData(ClientConfig.GetPersistentPath(GetStorageBoxKey()), Compressor.Compress(data));
		}

		public void ClearArchive()
		{
			for (int i = 0; i < Singleton<SceneModel>.Instance.mapConfig.Length; i++)
			{
				DeleteData(ClientConfig.GetPersistentPath(GetMapKey(Singleton<SceneModel>.Instance.mapConfig[i].id)));
			}
			DeleteData(ClientConfig.GetPersistentPath(GetStorageBoxKey()));
			DeleteData(ClientConfig.GetPersistentPath(GetBasicDataKey()));
			DeleteData(ClientConfig.GetPersistentPath(GetElevatorKey()));
			DeleteData(ClientConfig.GetPersistentPath(GetAmbulanceKey()));
		}

		public void SaveInt(string key, int val)
		{
			PlayerPrefs.SetInt(key, val);
		}

		public int GetInt(string key)
		{
			return PlayerPrefs.GetInt(key);
		}

		public void SaveFloat(string key, float val)
		{
			PlayerPrefs.SetFloat(key, val);
		}

		public float GetFloat(string key)
		{
			return PlayerPrefs.GetFloat(key);
		}

		public void SaveString(string key, string val)
		{
			PlayerPrefs.SetString(key, val);
		}

		public string GetString(string key)
		{
			return PlayerPrefs.GetString(key);
		}

		private void DeleteData(string path)
		{
			if (File.Exists(path))
			{
				File.Delete(path);
			}
		}

		private byte[] LoadData(string path)
		{
			if (File.Exists(path))
			{
				return File.ReadAllBytes(path);
			}
			return null;
		}

		private void WriteData(string path, byte[] bytes)
		{
			FileInfo fileInfo = new FileInfo(path);
			DirectoryInfo directory = fileInfo.Directory;
			if (!directory.Exists)
			{
				directory.Create();
			}
			FileStream fileStream = new FileStream(fileInfo.FullName, FileMode.Create);
			fileStream.Write(bytes, 0, bytes.Length);
			fileStream.Flush();
			fileStream.Close();
			fileStream.Dispose();
		}

		private string GetBasicDataKey()
		{
			return "basic_data_1.0";
		}

		private string GetMapKey(int id)
		{
			return "map" + id + "_1.0";
		}

		private string GetStorageBoxKey()
		{
			return "storagebox_1.0";
		}

		private string GetElevatorKey()
		{
			return "elevator_1.0";
		}

		private string GetAmbulanceKey()
		{
			return "ambulance_1.0";
		}
	}
}
